home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / VD08SMP.ZIP / usr / samples / textview / textview3.m < prev    next >
Encoding:
Text File  |  1996-02-13  |  3.5 KB  |  142 lines

  1. #include <pm/pm.h>
  2. #include <io.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5.  
  6. void readAndDisplayFile (char *fileName);
  7.  
  8. @interface Controller : Object
  9. {
  10. }
  11.  
  12. - windowDidResize: sender;
  13. - loadFile: sender;
  14.  
  15. @end
  16.  
  17. char        *contents;
  18. StdWindow   *window;
  19. Window      *mle;
  20.  
  21. @implementation Controller
  22.  
  23. - windowDidResize: sender
  24. {
  25.   [[sender findFromID: 1001] setSize: 0:0:[sender width]:[sender height]];
  26.   return self;
  27. }
  28.  
  29. - loadFile: sender
  30. {
  31.   FileDlg *fileDlg = [[FileDlg alloc] initForOpen: "Open File..."
  32.               withFilter: "*.*"];
  33.   if (([fileDlg runModalFor: window]) && ([fileDlg result] == DID_OK))
  34.     readAndDisplayFile ([fileDlg fileName]);
  35.  
  36.   [fileDlg free];
  37.   return self;
  38. }
  39.  
  40. @end
  41.  
  42. void readAndDisplayFile (char *fileName)
  43. {
  44.   char        *title;
  45.   FILE        *inputFile;
  46.   struct stat  statbuffer;
  47.  
  48.   if (stat (fileName,&statbuffer) < 0) { /* check file */
  49.     if (contents != NULL) {
  50.       free (contents);
  51.       contents = NULL;
  52.     }
  53.  
  54.     [mle setText: ""];
  55.     [window setTitle: "Textview: no file loaded"];
  56.     return;
  57.   }
  58.  
  59.   /*
  60.    * open file and read contents to buffer
  61.    */
  62.   inputFile = fopen (fileName,"r"); /* open text file read-only */
  63.       
  64.   contents = (char *) malloc (statbuffer.st_size + 1); /* allocate buffer */
  65.   fread (contents,statbuffer.st_size,1,inputFile); /* read contents of file */
  66.       
  67.   /*
  68.    * calculate title of window and set it
  69.    */
  70.   title = (char *) malloc (11 + /* this is the length of "Textview: " + 
  71.                    NULL */
  72.                strlen (fileName)); /* allocate buffer 
  73.                          for title */
  74.   sprintf (title,"Textview: %s",fileName); /* fill title buffer */
  75.   [mle setText: contents]; /* display contents of file */
  76.   [window setTitle: title];
  77.   free (title);
  78.   fclose (inputFile); /* close file */
  79.   
  80. }
  81.  
  82. main(int argc,char *argv[])
  83. {
  84.   StdApp      *application;
  85.   Controller  *controller;
  86.  
  87.   /*
  88.    * create app instance and window, create MLE for text display
  89.    */
  90.   application = [[StdApp alloc] init]; /* initialize application object */
  91.   window = [[MainWindow alloc] initWithId: 1000
  92.       andFlags: (FCF_SIZEBORDER | FCF_TITLEBAR |
  93.              FCF_SYSMENU | FCF_MINMAX |
  94.              FCF_SHELLPOSITION | FCF_TASKLIST |FCF_MENU)];
  95.   /* create main window */
  96.   controller = [[Controller alloc] init];
  97.   
  98.   [window createObjects]; /* create child windows of main window */
  99.   [window setDelegate: controller];
  100.  
  101.   [window bindCommand: 2001 withObject: controller 
  102.              selector: @selector (loadFile:)];
  103.   [window bindCommand: 2002 withObject: window
  104.              selector: @selector (performClose:)];
  105.  
  106.   mle = [[MultiLineEntryField alloc] initWithId: 1001 
  107.        andFlags: (WS_VISIBLE | //MLS_READONLY |
  108.           MLS_HSCROLL | MLS_VSCROLL)
  109.        in: window];
  110.   [window insertChild: mle]; /* insert MLE into window */
  111.  
  112.   contents = NULL;
  113.  
  114.   /*
  115.    * check for command line arguments and check given file (struct stat)
  116.    */
  117.   if (argc == 2) /* check for command line arguments, must be exactly one */
  118.     readAndDisplayFile (argv[1]);
  119.   else
  120.     [window setTitle: "Textview: no file loaded"];
  121.   
  122.   /*
  123.    * show window
  124.    */
  125.   [window makeKeyAndOrderFront: nil]; /* show window */
  126.  
  127.   /*
  128.    * run application
  129.    */
  130.   [application run];
  131.  
  132.   /*
  133.    * free all resources
  134.    */
  135.   if (contents != NULL)
  136.     free (contents); /* free contents buffer */
  137.  
  138.   [application free]; /* free application */
  139.   [window free]; /* free window */
  140.   [controller free]; /* free controller */
  141. }
  142.